//
//  HLLiveResultViewController.m
//  YueYue
//
//  Created by hulu001 on 2024/3/13.
//

#import "HLLiveResultViewController.h"
#import "HLLiveViewController.h"
#import "HLIMBackgroundView.h"
#import "HLUserInfoRequest.h"

@interface HLLiveResultViewController ()

@property (nonatomic, strong) HLLiveFemaleModel *liveFemale;
@property (nonatomic, strong) HLIMBackgroundView *backgroundView;
@property (nonatomic, strong) QMUIButton *closeButton;
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *lookLabel;
@property (nonatomic, strong) QMUIButton *followButton;
@property (nonatomic, strong) QMUIButton *chatButton;
@property (nonatomic, strong) QMUIButton *backButton;

@end

@implementation HLLiveResultViewController

- (instancetype)initWithLiveFemale:(HLLiveFemaleModel *)liveFemale
{
    self = [super init];
    if (self){
        self.liveFemale = liveFemale;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self followRequest];
}

- (void)initSubviews
{
    [super initSubviews];
    self.view.backgroundColor = UIColorBlack;
    
    NSMutableArray *controllers = [NSMutableArray array];
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if (![controller isKindOfClass:[HLLiveViewController class]]) {
            [controllers addObject:controller];
        }
    }
    self.navigationController.viewControllers = controllers;
    [self.view addSubview:self.backgroundView];
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"square_post_change_close") forState:UIControlStateNormal];
    [_closeButton addTarget:self action:@selector(closeEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(40);
        make.top.mas_equalTo(StatusBarHeight + 10);
        make.right.mas_equalTo(-10);
    }];
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 30;
    [self.view addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(60);
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_closeButton.mas_bottom).offset(20);
    }];
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:_liveFemale.femaleAvatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    
    self.nicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(14) textColor:UIColorWhite];
    _nicknameLabel.text = _liveFemale.femaleNickName;
    [self.view addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_avatarView);
        make.top.equalTo(_avatarView.mas_bottom).offset(8);
    }];
    
    self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(18) textColor:UIColorWhite];
    _titleLabel.text = @"主播累了，休息好了再来哦";
    [self.view addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_nicknameLabel.mas_bottom).offset(24);
    }];
    
    UIView *leftLineView = [[UIView alloc] init];
    leftLineView.backgroundColor = HLTextColor3;
    [self.view addSubview:leftLineView];
    [leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.equalTo(_titleLabel.mas_left).offset(-12);
        make.centerY.equalTo(_titleLabel);
        make.height.mas_equalTo(1);
    }];
    
    UIView *rightLineView = [[UIView alloc] init];
    rightLineView.backgroundColor = HLTextColor3;
    [self.view addSubview:rightLineView];
    [rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.equalTo(_titleLabel.mas_right).offset(12);
        make.centerY.equalTo(_titleLabel);
        make.height.mas_equalTo(1);
    }];
    
    self.lookLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(14) textColor:HLTextColor3];
    _lookLabel.text = [NSString stringWithFormat:@"%@ 人看过", _liveFemale.liveAudienceCount];
    [self.view addSubview:_lookLabel];
    [_lookLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_titleLabel.mas_bottom).offset(12);
    }];
    
    self.followButton = [[QMUIButton alloc] init];
    [_followButton addTarget:self action:@selector(followAction) forControlEvents:UIControlEventTouchUpInside];
    [_followButton setTitle:@"加个关注" forState:UIControlStateNormal];
    [_followButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_followButton setBackgroundColor:HLMainColor];
    _followButton.cornerRadius = 28;
    _followButton.titleLabel.font = UIFontMediumMake(16);
    [self.view addSubview:_followButton];
    [_followButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(56);
        make.top.equalTo(_lookLabel.mas_bottom).offset(40);
    }];
    
    
    self.chatButton = [[QMUIButton alloc] init];
    [_chatButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    [_chatButton setTitle:@"撩一下" forState:UIControlStateNormal];
    [_chatButton setTitleColor:HLMainColor forState:UIControlStateNormal];
    [_chatButton setBackgroundColor:UIColorWhite];
    _chatButton.cornerRadius = 28;
    _chatButton.titleLabel.font = UIFontMediumMake(16);
    [self.view addSubview:_chatButton];
    _chatButton.hidden = YES;
    [_chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(56);
        make.top.equalTo(_lookLabel.mas_bottom).offset(40);
    }];
    
    self.backButton = [[QMUIButton alloc] init];
    [_backButton addTarget:self action:@selector(closeEvent) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setTitle:@"返回首页" forState:UIControlStateNormal];
    [_backButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    _backButton.cornerRadius = 28;
    _backButton.layer.borderWidth = 1;
    _backButton.layer.borderColor = HLTextColor3.CGColor;
    _backButton.titleLabel.font = UIFontMediumMake(16);
    [self.view addSubview:_backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(56);
        make.top.equalTo(_followButton.mas_bottom).offset(16);
    }];
    
}

- (HLIMBackgroundView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[HLIMBackgroundView alloc] initWithFrame:UIScreen.mainScreen.bounds];
        _backgroundView.backgroundColor = UIColorBlack;
        [_backgroundView.imageView sd_setImageWithURL:[NSURL URLWithString:_liveFemale.femaleAvatar] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
    }
    return _backgroundView;
}



- (void)followRequest
{
    [[[HLUserInfoRequest alloc] initWithUserId:_liveFemale.femaleUserId] startWithCompletionBlockWithSuccess:^(__kindof HLUserInfoRequest * _Nonnull request) {
        
        if (request.isSuccess) {
            HLUserInfoModel *user = [request user];
            if (![user.followType isEqualToString:@"0"]) {
                self.followButton.hidden = NO;
                self.chatButton.hidden = YES;
                
            } else {
                self.followButton.hidden = YES;
                self.chatButton.hidden = NO;
            }
        }
        
    } failure:nil];
}

- (void)chatAction
{
    self.chatButton.enabled = NO;
    [HLUIHelper chatWithUserId:self.liveFemale.femaleUserId controller:self];
    NSMutableArray *controllers = [NSMutableArray array];
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if (![controller isKindOfClass:[HLLiveResultViewController class]]) {
            [controllers addObject:controller];
        }
    }
    self.navigationController.viewControllers = controllers;
   
}

- (void)followAction
{

    [HLUIHelper followWithUserId:self.liveFemale.femaleUserId follow:NO callback:^(BOOL isConfirm) {
        if (isConfirm){
            [self followRequest];
        }
    }];
}

- (void)closeEvent
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture
{
    return YES;
}
- (BOOL)forceEnableInteractivePopGestureRecognizer
{
    return NO;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}

@end
